package com.hex.ds.hdrs.center.controller;


import com.hex.bigdata.hbda.annotation.AnonymousAccess;
import com.hex.bigdata.hbda.annotation.Log;
import com.hex.bigdata.hbda.base.Page;
import com.hex.ds.hdrs.center.po.CenterConf;
import com.hex.ds.hdrs.center.service.CenterConfService;
import com.hex.ds.hdrs.center.service.CenterService;
import com.hex.ds.hdrs.common.catalogue.Catalogue;
import com.hex.ds.hdrs.common.page.PageData;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;

/**
 * <p>
 * 中心定义表 前端控制器
 * </p>
 *
 * @author tj
 * @since 2021-01-08
 */
@Slf4j
@RestController
@RequestMapping("/api/centerDef")
public class CenterConfController {

    @Autowired
    private CenterConfService centerConfService;

    @PostMapping("/centerDef")
    @ApiOperation("添加中心配置")
    @Log("中心新增")
    public ResponseEntity<Object> add(@RequestBody CenterConf centerConf) {
        log.info("进入了CenterConfController的add方法");
        this.centerConfService.add(centerConf);
        return new ResponseEntity<>("新增成功！", HttpStatus.OK);
    }

    @PutMapping("/centerDef")
    @ApiOperation("修改中心配置")
    @Log("中心修改")
    public ResponseEntity<Object> update(@RequestBody CenterConf centerConf) {
        log.info("进入了CenterConfController的update方法");
        this.centerConfService.update(centerConf);
        return new ResponseEntity<>("修改成功！", HttpStatus.OK);
    }

    @DeleteMapping("/centerDef")
    @ApiOperation("删除中心配置")
    @Log("中心删除")
    public ResponseEntity<Object> delete(@RequestBody List<String> list) {
        log.info("进入了CenterConfController的delete方法");
        this.centerConfService.delete(list);
        return new ResponseEntity<>("删除成功！", HttpStatus.OK);
    }

    @GetMapping("/selectPages")
    @ApiOperation("查询中心配置")
    public ResponseEntity select(CenterConf centerConf, Page page) {
        log.info("进入了CenterConfController的select方法");
        PageData pageData = new PageData(this.centerConfService.selectAll(centerConf, page), page);
        return new ResponseEntity<>(pageData, HttpStatus.OK);
    }

    @GetMapping("/selectCenterInf")
    @ApiOperation("查询中心配置信息")
    public ResponseEntity selectCenterInf() {
        log.info("进入了CenterConfController的selectCenterInf方法");
        return new ResponseEntity<>(this.centerConfService.selectCenterInf(), HttpStatus.OK);
    }

    @GetMapping("/initMasterInfo")
    @ApiOperation("初始化中心基本信息")
    public ResponseEntity initMasterInfo() {
        log.info("进入了CenterConfController的initMasterInfo方法");
        return new ResponseEntity<>(this.centerConfService.initMasterInfo(), HttpStatus.OK);
    }

    @Log("判断中心编码或名称是否重复")
    @ApiOperation("判断中心编码或名称是否重复")
    @PostMapping(value = "/checkCenterData")
    public ResponseEntity<Object> checkCenterData(@RequestBody CenterConf centerConf) {
        log.info("【CenterConfController的checkCenterData】：{}", centerConf.toString());
        boolean result = this.centerConfService.checkCenterData(centerConf);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    /**
     * @Method: getLogFilePath
     * @Param: []
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object>
     * @Description: 获取日志文件目录
     * @Author: ji.tang
     * @Date: 2023/3/22 17:17
     * @Version: V1.0
     */
    @AnonymousAccess
    @GetMapping("/getLogFilePath")
    @ApiOperation("获取日志文件目录")
    public ResponseEntity<Object> getLogFilePath() {
        log.debug("进入【CenterConfController.getLogFilePath】");
        return new ResponseEntity<>(centerConfService.getLogFilePath(), HttpStatus.OK);
    }

    /**
     * @Method: getLogFileInfo
     * @Param: [catalogue]
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object>
     * @Description: 获取日志文件内容
     * @Author: ji.tang
     * @Date: 2023/3/22 17:17
     * @Version: V1.0
     */
    @AnonymousAccess
    @PostMapping("/getLogFileInfo")
    @ApiOperation("获取日志文件内容")
    public ResponseEntity<Object> getLogFileInfo(@RequestBody Catalogue catalogue) {
        log.debug("进入【CenterCo.nfController.getLogFileInfo】参数:{}", catalogue.toString());
        return new ResponseEntity<>(centerConfService.getLogFileInfo(catalogue), HttpStatus.OK);
    }

    /**
     * @Method: downLogFile
     * @Param: [catalogue, response, request]
     * @Return: org.springframework.http.ResponseEntity<java.lang.Object>
     * @Description: 下载日志文件
     * @Author: ji.tang
     * @Date: 2023/3/23 9:26
     * @Version: V1.0
     */
    @AnonymousAccess
    @PostMapping({"/downLogFile"})
    @ApiOperation("下载日志文件")
    public ResponseEntity<Object> downLogFile(@RequestBody Catalogue catalogue) throws IOException {
        log.debug("进入【CenterConfController.downLogFile】参数:{}", catalogue.toString());
        byte[] bytes = Files.readAllBytes(new File(catalogue.getFilePath()).toPath());
        ByteArrayResource bar = new ByteArrayResource(bytes);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header("Content-disposition", "attachment; filename=log.log")
                .body(bar);
    }
}

